package com.heking.aoplib;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.zhoug.aop.AopLib;
import com.zhoug.aop.annotation.ClickLimit;
import com.zhoug.aop.annotation.NeedLogin;
import com.zhoug.aop.interfaces.ILoginFilter;
import com.zhoug.common.utils.LogUtils;
import com.zhoug.widget.dialog.ButtonDialog;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private static final String TAG = ">>>MainActivity";
    private TextView mBtn1;
    private Button mBtn2;
    private Button mBtn3;
    private Button mBtn4;
    private Button mBtn5;
    private Button mBtn6;
    private Button mBtn7;
    private Button mBtn8;
    private Button mBtn9;
    private Button mBtn71;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViews();
        addListener();
        AopLib.setLoginFilter(new LoginFilter());
    }

    private void findViews() {
        mBtn1 = findViewById(R.id.btn1);
        mBtn2 = findViewById(R.id.btn2);
        mBtn3 = findViewById(R.id.btn3);
        mBtn4 = findViewById(R.id.btn4);
        mBtn5 = findViewById(R.id.btn5);
        mBtn4.setOnClickListener(this);
        mBtn5.setOnClickListener(this);
        mBtn6 = findViewById(R.id.btn6);
        mBtn7 = findViewById(R.id.btn7);
        mBtn8 = findViewById(R.id.btn8);
        mBtn9 = findViewById(R.id.btn9);
        mBtn71 = findViewById(R.id.btn7_1);
    }

    private void addListener(){
        mBtn1.setOnClickListener(new View.OnClickListener() {
            @ClickLimit
            @Override
            public void onClick(View v) {
                Log.e(TAG, "点击btn1");
                toast("点击btn1");

            }
        });
        mBtn2.setOnClickListener(new View.OnClickListener() {
            @ClickLimit(2000)
            @Override
            public void onClick(View v) {
                Log.e(TAG, "点击btn2");
                toast("点击btn2");
            }
        });
        mBtn3.setOnClickListener(new View.OnClickListener() {
            @ClickLimit
            @Override
            public void onClick(View v) {
                Log.e(TAG, "点击btn3");
                toast("点击btn3");

            }
        });
        mBtn6.setOnClickListener(new View.OnClickListener() {
            @NeedLogin(value = NeedLogin.SHOW_TOAST,tipToast = "请登录")
            @Override
            public void onClick(View v) {
                Log.e(TAG, ">>>>>>>>>>>>>>>>>>>mBtn6");
                toHome();
            }
        });
        mBtn7.setOnClickListener(new View.OnClickListener() {
            @NeedLogin(value = NeedLogin.SHOW_DIALOG,tipDialog = "是否登录")
            @Override
            public void onClick(View v) {
                Log.e(TAG, ">>>>>>>>>>>>>>>>>>>mBtn7");
                toHome();
            }
        });
        mBtn71.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                btn71();
            }
        });
        mBtn8.setOnClickListener(new View.OnClickListener() {
            @NeedLogin(value = NeedLogin.TO_LOGIN)
            @Override
            public void onClick(View v) {
                Log.e(TAG, ">>>>>>>>>>>>>>>>>>>mBtn8");
                toHome();
            }
        });
        mBtn9.setOnClickListener(new View.OnClickListener() {
            @NeedLogin(value = NeedLogin.NO_RESPONSE)
            @Override
            public void onClick(View v) {
                Log.e(TAG, ">>>>>>>>>>>>>>>>>>>mBtn9");
                toHome();
            }
        });
    }

    @ClickLimit
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn4:
                Log.e(TAG, "点击btn4");
                toast("点击btn4");

                break;
            case R.id.btn5:
                Log.e(TAG, "点击btn5");
                toast("点击btn5");
                break;
        }
    }

    @NeedLogin(value = NeedLogin.SHOW_DIALOG,tipDialog = "是否登录71")
    private void btn71(){
        Log.e(TAG, ">>>>>>>>>>>>>>>>>>>btn71");
        toHome();
    }

    private void toHome(){
        Log.e(TAG, ">>>>>>>>>>>>>>>>>>>登录成功后执行");
        toast("登录成功后执行");

    }

    private class  LoginFilter implements ILoginFilter{

        @Override
        public boolean isLogin() {
            return false;
        }

        @Override
        public void showToast(String tipToast) {
            toast(tipToast);
        }

        @Override
        public void showDialog(Context context, String tipDialog) {
            final ButtonDialog buttonDialog = new ButtonDialog(MainActivity.this, tipDialog);
            buttonDialog.setCancelable(true);
            buttonDialog.setCanceledOnTouchOutside(true);
            buttonDialog.setOnclickListener1("取消", new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    buttonDialog.cancel();
                }
            });
            buttonDialog.setOnclickListener2("登录", new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    buttonDialog.cancel();
                    _toLogin();

                }
            });
            buttonDialog.show();
        }

        @Override
        public void toLogin(Context context) {
            _toLogin();
        }
    }

    private void _toLogin(){
        Intent intent = new Intent(MainActivity.this, LoginActivity.class);
        intent.putExtra(LoginActivity.EXTRA_FOR_RESULT,true);
        MainActivity.this.startActivityForResult(intent,121);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==121 && resultCode==RESULT_OK && data!=null){
            boolean booleanExtra = data.getBooleanExtra(LoginActivity.OUT_PUT_LOGIN_SUCCESS, false);
            if(booleanExtra){
                toHome();
            }
        }
    }

    private void toast(String msg){
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }
}